import {buildRes, saveI18n} from "./parse-i18n-tool"
import {PluginOption} from "vite";

export const awsI18nUtil = (AWSDevParams: any, isDo = false, isDesign = false) => {
    if (!isDo) {
        return {}  as PluginOption;
    }
    const context = {
        AWSDevParams,
    }
    const count = {k:0};
    return {
        buildEnd(options, op) {
            return new Promise((resolve, reject) => {
                let oldCount = -1;
                const sss = setInterval(()=>{
                    //解决buildEnd不能在transform完全后执行问题
                    if(count.k == oldCount){
                        clearInterval(sss);
                        saveI18n(context, isDesign);
                        console.log("运行结束")
                    }else{
                        oldCount = count.k;
                    }
                },1000)
            })
        },
        transform(code, id) {
            count.k++;
            if (id.endsWith(".vue") || id.endsWith(".ts")) {
                buildRes(code);
            }
        },
    } as PluginOption
}
